package com.datatools.entity;

import com.mysql.jdbc.Driver;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 数据库实体类
 */
public class ConnectionInfoEntity implements Serializable {
    private static final long serialVersionUID = 1L;
    //连接名称
    private String conName;
    //数据库类型
    private DataBaseTypeEnum dbType;
    //连接IP
    private String ip;
    //端口
    private String port;
    //数据名称
    private String dbName;
    //用户
    private String userName;
    //密码
    private String password;
    //数据库连接 不用序列化的字段
    private transient Connection connection;

    private transient StringBuffer errorSb;

    //树形控件需要重写数据对象的toString方法
    @Override
    public String toString() {
        return this.conName;
    }

    public DataBaseTypeEnum getDbType() {
        return dbType;
    }

    public void setDbType(DataBaseTypeEnum dbType) {
        this.dbType = dbType;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }

    public String getDbName() {
        return dbName;
    }

    public void setDbName(String dbName) {
        this.dbName = dbName;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Connection getNewConnection() {
        Connection con = null;
        errorSb = new StringBuffer();
        try {
            String url = "";
            //TODO 支持更多数据库 JDBC 连接方式
            switch (this.dbType) {
                case MYSQL:
                    //mysql8 之前的驱动版本
                    url = "jdbc:mysql://" + ip + ":" + port + "/" + dbName + "?useSSL=false";
                    break;
                case SQLSERVER:
                    break;
                case ORACLE:
                    break;
            }
            Driver mysqlDriver = new Driver();
            DriverManager.registerDriver(mysqlDriver);
            con = DriverManager.getConnection(url, userName, password);
        } catch (SQLException e) {
            StackTraceElement[] stackTraceElements = e.getStackTrace();
            errorSb.append("获取连接异常：\n");
            for (StackTraceElement stackTraceElement : stackTraceElements) {
                errorSb.append("at ");
                errorSb.append(stackTraceElement.getClassName());
                errorSb.append(".");
                errorSb.append(stackTraceElement.getMethodName());
                errorSb.append("(");
                errorSb.append(stackTraceElement.getFileName());
                errorSb.append(":");
                errorSb.append(stackTraceElement.getLineNumber());
                errorSb.append(")");
                errorSb.append("\n");
            }
            e.printStackTrace();
        }
        if (null == con || errorSb.length() > 0) {
            if (errorSb.length() == 0) {
                errorSb.append("获取连接失败！");
                this.errorSb = errorSb;
            }
        }
        return con;
    }

    public Connection getConnection() {
        if (this.connection == null) {
            this.connection = getNewConnection();
        }
        return this.connection;
    }

    public StringBuffer getErrorSb() {
        return errorSb;
    }

    public void closeConnection() {
        if (null != connection) {
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public String getConName() {
        return conName;
    }

    public void setConName(String conName) {
        this.conName = conName;
    }

}
